'use strict';

const Controller = require('egg').Controller;
const Op = require('sequelize').Op;

class OrderController extends Controller {
  async addCar() {
    const {
      ctx,
    } = this;
    const shopInfo = ctx.request.body;
    const result = await ctx.service.order.addCar(shopInfo);
    ctx.body = result;
  }
  async getCar() {
    const {
      ctx,
    } = this;
    const uid = ctx.params.userid;
    const result = await ctx.model.TbOrder.findAll({
      where: {
        [Op.and]: [{
          uid,
        },
        {
          car: true,
        },
        ],
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          result,
        },
      };
      return;
    }
    ctx.body = {
      status: 400,
    };
  }
  async delCar() {
    const {
      ctx,
    } = this;
    const id = ctx.params.id;
    const result = await ctx.model.TbOrder.destroy({
      where: {
        id,
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          msg: '移除购物车成功',
        },
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: {
        msg: '移除购物车失败',
      },
    };
  }
  async upOrderCar() {
    const {
      ctx,
    } = this;
    const numInfo = ctx.request.body;
    const result = await ctx.model.TbOrder.update({
      num: numInfo.num,
    }, {
      where: {
        id: numInfo.id,
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: {
        msg: '修改数量失败，请重试',
      },
    };
  }
  async upCarCheck() {
    const {
      ctx,
    } = this;
    const carinfo = ctx.request.body;
    const result = await ctx.model.TbOrder.update({
      carcheck: carinfo.flag,
    }, {
      where: {
        id: carinfo.id,
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
      };
      return;
    }
    ctx.body = {
      status: 400,
    };
  }
  async upAllCarCheck() {
    const {
      ctx,
    } = this;
    const checkinfo = ctx.request.body;
    const result = await ctx.model.TbOrder.update({
      carcheck: checkinfo.flag,
    }, {
      where: {
        uid: checkinfo.uid,
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
      };
      return;
    }
    ctx.body = {
      status: 400,
    };
  }
  async setOrder() {
    const {
      ctx,
    } = this;
    const orderInfo = ctx.request.body;
    const result = await ctx.service.order.setOrder(orderInfo);
    ctx.body = result;
  }
  async setState() {
    const {
      ctx,
    } = this;
    const orderState = ctx.request.body;
    const result = [];
    for (const item of orderState.id) {
      const res = await ctx.model.TbOrder.update({
        state: orderState.state,
      }, {
        where: {
          id: item,
        },
      });
      result.push(res);
    }
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          msg: '订单支付成功',
        },
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: {
        msg: '订单支付失败，请重试',
      },
    };
  }
  async getOrder() {
    const {
      ctx,
    } = this;
    const userid = ctx.params.userid;
    const pagesize = ctx.params.pagesize;
    const page = ctx.params.page;
    const result = await ctx.service.order.getOrders(userid, pagesize, page);
    ctx.body = result;
  }
  async delOrder() {
    const {
      ctx,
    } = this;
    const ordernumber = ctx.params.ordernumber;
    console.log(ordernumber);
    const result = await ctx.model.TbOrder.destroy({
      where: {
        ordernumber,
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          msg: '删除订单成功',
        },
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: {
        msg: '删除订单失败，请刷新后重试',
      },
    };
  }
  async getstateOrder() {
    const {
      ctx,
    } = this;
    const userid = ctx.params.userid;
    const pagesize = ctx.params.pagesize;
    const page = ctx.params.page;
    const state = ctx.params.state;
    const result = await ctx.service.order.getStateOrders(userid, pagesize, page, state);
    ctx.body = result;
  }
  async getOrderCount() {
    const {
      ctx,
    } = this;
    const uid = ctx.params.uid;
    const result = await ctx.model.TbOrder.findAndCountAll({
      where: {
        uid,
        [Op.and]: {
          ordernumber: {
            [Op.not]: null,
          },
        },
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          count: result.count,
        },
      };
      return;
    }
    ctx.body = {
      state: 400,
      data: {
        msg: '订单数据获取失败，请刷新重试',
      },
    };
  }
  async getOrderId() {
    const {
      ctx,
    } = this;
    const id = ctx.params.id;
    const result = await ctx.model.TbOrder.findOne({
      where: {
        id,
        [Op.and]: {
          ordernumber: {
            [Op.not]: null,
          },
        },
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          result,
        },
      };
      return;
    }
    ctx.body = {
      state: 400,
      data: {
        msg: '订单数据获取失败，请刷新重试',
      },
    };
  }
}
module.exports = OrderController;
